﻿using System;
using System.Drawing;

namespace MassPokerTool.Infrastructure.Hooking.Hooks
{
    public class BitBltHookHandler : IBitBltHookHandler
    {
        public void Handle(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, int rasterOp)
        {
            try
            {
                var myhMemDc = PlatformInvoke.CreateCompatibleDC(hdcSource);
                var myhBitmap = PlatformInvoke.CreateCompatibleBitmap(hdcSource, wDest, hDest);
                if (myhBitmap == IntPtr.Zero)
                    throw new ApplicationException("myhBitmap == IntPtr.Zero");

                var myhOld = PlatformInvoke.SelectObject(myhMemDc, myhBitmap);
                PlatformInvoke.BitBlt(myhMemDc, 0, 0, wDest, hDest, hdcSource, 0, 0, PlatformInvoke.Srccopy);
                var bmp = Image.FromHbitmap(myhBitmap);
                bmp.Save("C:\\Temp\\" + Guid.NewGuid() + ".bmp");
                PlatformInvoke.SelectObject(myhMemDc, myhOld);
                PlatformInvoke.DeleteDC(myhMemDc);

                PlatformInvoke.DeleteObject(myhBitmap);
            }
            catch (Exception e)
            {
                //_queue.Push(e.ToString());
            }
        }
    }
}
